/*
 * @Author: lerio.cheng
 * @Date:   2017-02-22 16:44:17
 * @Last Modified by:   DELL
 * @Last Modified time: 2017-02-24 12:49:10
 */

'use strict';

app.controller('batchImportCtrl', function($element,$scope, FileUploader, uploadAddress, signature, tipService, $compile, $window, $http, rest, $state, ngDialog, user, $location,uploadConf,uploadConf2,mapSev,uiLoad,$rootScope) {

    var uploader = $scope.uploader = new FileUploader({
        url: uploadAddress
    });

    function time(mydate){
      var str = mydate.substring(0,10);
      var dataArr = str.split('-');
      var timeStr = dataArr[0]
        +"年"+dataArr[1]+"月"+dataArr[2]
        +"日";
        return timeStr;
    }

    /* 列表按钮的模板，入参：{
        isDisabled: true or false, 按钮是否禁用
        functionName: 按钮ng-click调用函数名
        paramId: ng-click传参，一般为各种id
        buttonText: 按钮文字
    } */
    var btntmp = function (opt) {
        var wrap = document.createElement('div'), abtn = document.createElement('a');
        wrap.appendChild(abtn);
        $(abtn).attr('href', 'javascript:;').addClass('btn');
        // wrap.appendChild(imgIcon);
        // $(imgIcon).attr('src', 'img/houseIcon.png');
        // $(imgIcon).addClass('houseIcon');
        if (opt.isDisabled) {
            $(abtn).addClass('disabled');
        }
        $(abtn).text(opt.buttonText);
        if (opt.functionName) {
            var ngClick = opt.functionName + '($event, "'+opt.paramId+'")';
            $(abtn).attr('ng-click', ngClick);
        }
        return $(wrap).html();
    }

    var getButtons = function (opt) {

        var getBtn = function (opt) { // 房源恩扭
            if (opt.decorate.decorateStatus >= 1) {// 装修状态判断
                  return btntmp({
                    isDisabled: false,
                    paramId: opt.houseId,
                    functionName: "modifyInfo",
                    buttonText: "修改信息"
                });
            }else{
                return btntmp({
                    isDisabled: false,
                    paramId: opt.houseId,
                    functionName: "supplementPic",
                    buttonText: "补充照片"
                });
            }

        }

        return [
            getBtn(opt)
        ];

    }

    $scope.dataTableSettings = {
        "sAjaxSource": "4008",
        "aoColumns": [
            {
                "mDataProp": "",
                "sTitle": "行号",
                "sDefaultContent": "",
                "sClass": "center"
            },
            {
                "mDataProp": "",
                "sTitle": "房间号",
                "sDefaultContent": "",
                "sClass": "center"
            }, {
                "mDataProp": "",
                "sTitle": "导入状态",
                "sDefaultContent": "",
                "sClass": "center"
            }, {
                "mDataProp": "",
                "sTitle": "导入说明",
                "sDefaultContent": "",
                "sClass": "center"
            }, {
                "mDataProp": "operate",
                "sTitle": "操作",
                "sDefaultContent": "",
                "sClass": "center handle-td operation"
            }
        ],
        "fnRowCallback": function(nRow, aData, iDisplayIndex) {
            var btnStr = "";
            var houseBtnStr = "";
            var contractBtnStr = "";
            var decorateBtnStr = "";
            var failureStr = "";
            var failureStr2 = "";
            var buttons = getButtons(aData);

            var btnStr = buttons.join("");

            // $(nRow).find(".handle-td").append($compile(houseBtnStr)($scope));
            // $(nRow).find(".handle-td").append($compile(contractBtnStr)($scope));
            // $(nRow).find(".handle-td").append($compile(decorateBtnStr)($scope));
            $(nRow).find(".handle-td").append($compile(btnStr)($scope));
            $(nRow).find(".handle-td").prev().empty().append(time(aData.createDate));
            $(nRow).find(".failure").empty().append($compile(failureStr)($scope));

        },
        "fnDrawCallback": function(oSettings) {

            if ("number" == typeof $scope.houseLoanStatusValue) {
                $scope.checkTypeArray.map(function(ele, index) {
                    if ($scope.houseLoanStatusValue == ele.value) {
                        $scope.houseLoanStatus.selected = ele;
                    }
                })
            }
        },
        "fnServerParams": function(aoData) {
            if($scope.houseLoanStatus && $scope.houseLoanStatus.selected){
                $scope.queryParams.houseLoanStatus = $scope.houseLoanStatus.selected.value
            }

            aoData.push({
                'name': 'queryParams',
                'value': $scope.queryParams
            });
            return aoData;
        }
    }

    $scope.initFinish = function(dataTable) {
         $scope.dataTable = dataTable;
    }

    $scope.modifyInfo = function() {
        console.log( "modifyInfo" );
    }

    $scope.supplementPic = function() {
        console.log( "supplementPic" );
    }

    $scope.addPicModel = function() {
        console.log( "addModelList" );
        $state.go('app.houseOwner.housePicsTemplet');
    }

    $scope.downloadModel = function() {
        console.log( "downloadModel" );
    }

    $scope.importHouseInfo = function() {
        console.log( "importHouseInfo" );
    }

    $scope.importAgain = function() {
        console.log( "importAgain" );
    }

    $scope.choiseFile = function() {
        $('#uploaderHouseFileInput').click();
    }

    uploader.filters.push({
        name: 'customFilter',
        fn: function(item /*{File|FileLikeObject}*/ , options) {
            return this.queue.length < 10;
        }
    });

    uploader.onWhenAddingFileFailed = function(item /*{File|FileLikeObject}*/ , filter, options) {
        //console.info('onWhenAddingFileFailed', item, filter, options);
    };
    uploader.onAfterAddingFile = function(fileItem) {
        console.info('onAfterAddingFile', fileItem);
        $scope.fileName = fileItem.file.name;
    };
    uploader.onAfterAddingAll = function(addedFileItems) {
        // console.info('onAfterAddingAll', addedFileItems);
    };
    uploader.onBeforeUploadItem = function(item) {
        return item.formData = [signature.run('9999', {})];
       // console.info('onBeforeUploadItem', item);
    };
    uploader.onProgressItem = function(fileItem, progress) {
        // console.info('onProgressItem', fileItem, progress);
    };
    uploader.onProgressAll = function(progress) {
        // console.info('onProgressAll', progress);
    };
    uploader.onSuccessItem = function(fileItem, response, status, headers) {
        console.info('onSuccessItem', fileItem, response, status, headers);
        // console.info('uploader', uploader);
    };
    uploader.onErrorItem = function(fileItem, response, status, headers) {
        console.info('onErrorItem', fileItem, response, status, headers);
    };
    uploader.onCancelItem = function(fileItem, response, status, headers) {
        //console.info('onCancelItem', fileItem, response, status, headers);
    };
    uploader.onCompleteItem = function(fileItem, response, status, headers) {
        // console.info('onCompleteItem', fileItem, response, status, headers);
        // if($scope.indexSelect != -1){
        //     $scope.imgNew[$scope.typeSelect].value.splice($scope.indexSelect, 1, response.res.data.x[0] );
        // }else{
        //     $scope.imgNew[$scope.typeSelect].value.push (response.res.data.x[0]) ;
        // }
        $('#uploaderFileInput').val('');
    };
    uploader.onCompleteAll = function() {
    };

    $scope.makeSureConfirm = function() {
        // $scope.confirm($scope.uploader);
    }
    $scope.readFile = function(){
            setTimeout("$('#itemUpload').click();",20);
    }


});
